<?php 

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class UserController extends Controller
{

	public function add(){
		return view('user.add');
	}

    public function store(Request $request){
        $input = $request->except('_token');
        $input['password'] = md5($input['password']);
        $res = User::create($input);
        if($res){
            return redirect('user/index');
        }else{
            return back();
        }

    }

    public function index(){
	    $user = User::get();

	    return view('user.list',compact('user'));
    }

    public function edit($id){
	    $user = User::find($id);
	    return view('user.edit',compact('user'));
    }
    public function update(Request $request){
        $input = $request->all();
        $user = User::find($input['id']);
        $res = $user->update(['username'=>$input['username']]);
        if($res){
            return redirect('user/index');
        }else{
            return back();
        }
    }

    public function destroy($id){
        $user = User::find($id);
        $res = $user->delete();
        if($res){
            $data = [
                'status'=>0,
                'message'=>'删除成功'
            ];
        }else{
            $data= [
                'status'=>1,
                'message'=>'删除失败'
            ];
        }
        return $data;
    }



}